<template>
  <div class="count">
    <a class="minus" @click="minus">-</a>
    <span class="num">{{ num }}</span>
    <a class="plus" @click="plus">+</a>
  </div>
</template>

<script>
import { mapGetters, mapActions } from 'vuex'

export default {
  props: ['info'],
  data () {
    return {
      goods: {},
      num: 0
    }
  },
  computed: {
    ...mapGetters('cart', [
      'getCart'
    ])
  },
  created () {
    this.goods = this.info
  },
  methods: {
    minus () {
      if (this.info.num > 0) {
        this.info.num -= 1
      }
    },
    plus () {
      this.info.num += 1
      if (this.isInCart(this.info)) {
        this.changeNewVal(this.info)
      } else {
        this.pushNew(this.info)
      }
    },
    isInCart (param) {
      if (this.getCart.length > 0) {
        var cartList = this.getCart
        return cartList.find((item) => item.id === param.id)
      } else {
        return false
      }
    },
    ...mapActions('cart', [
      'pushNew',
      'changeNewVal'
    ])
  },
  watch: {
  }
}
</script>


<style lang="stylus" scoped>
.count
  a
    display: inline-block
    font-size: 36px
    width: 48px
    height: 48px
    line-height 48px
    text-align: center
  a.minus
    background-color: #fff
    border: 1px solid #f90
    color: #f90
  a.plus
    background-color: #f90
    color: #fff
  span
    margin: 0 15px
</style>

